﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 菜单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MenuController : ControllerBase
    {
        public MenuController(IMenuitemRepository menuitemService)
        {
            MenuitemService = menuitemService;
        }

        public IMenuitemRepository MenuitemService { get; }   

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuList(string? mname = "", int status = 2)
        {
            ApiResult apiResult = new ApiResult();
            var obj = MenuitemService.GetMenuList(mname, status);
            if (obj.Count > 0)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功";
                apiResult.count = obj.Count;
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败";
            return Ok(apiResult);
        }

        /// <summary>
        /// 左侧穿梭框显示
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetLeftMenuActionList(int mid = 0)
        {
            ApiResult apiResult = new ApiResult();
            var obj = MenuitemService.GetLeftMenuActionList(mid);
            if (obj.Count > 0)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功";
                apiResult.count = obj.Count;
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败";
            return Ok(apiResult);
        }

        /// <summary>
        /// 右侧穿梭框显示
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRightMenuActionList(int mid = 0)
        {
            ApiResult apiResult = new ApiResult();
            var obj = MenuitemService.GetRightMenuActionList(mid);
            if (obj!=null)
            {
                apiResult.code = "200";
                apiResult.message = "获取成功";
                apiResult.count = obj.Count;
                apiResult.data = obj;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "获取失败！Id可能存在问题！";
            return Ok(apiResult);
        }

        /// <summary>
        /// 添加菜单按钮关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuAction(TMenuAction m)
        {
            ApiResult apiResult = new ApiResult();
            bool IsOK = MenuitemService.AddMenuAction(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "添加成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "已经存在该账号！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMenuItem(int mid)
        {
            if(mid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = MenuitemService.DelMenuItem(mid);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "删除成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "删除失败！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuItem(TMenu m)
        {
            ApiResult apiResult = new ApiResult();
            bool IsOK = MenuitemService.AddMenuItem(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "添加成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "添加失败！已存在该菜单！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdMenuItem(TMenu m)
        {
            if(m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = MenuitemService.UpdMenuItem(m);
            if (IsOK)
            {
                apiResult.code = "200";
                apiResult.message = "修改成功！";
                apiResult.data = true;
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message = "修改失败！已存在该菜单！";
            apiResult.data = false;
            return Ok(apiResult);
        }

        /// <summary>
        /// 分配权限列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuActionList(int rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = MenuitemService.GetMenuActionList(rid);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleMenuAction(CRoleMenuAction m)
        {
            if(m.Rid<=0)
            {
                return Ok(BaseResult.Result(true, "添加失败！", "400"));
            }
            bool IsOK = MenuitemService.AddRoleMenuAction(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！", "500"));
        }

        [HttpGet]
        public IActionResult GetMenuitemAction(int uid)
        {
            if (uid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            var obj = MenuitemService.GetMenuitemAction(uid);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }
    }
}
